// pages/auth/uploadImg.js
const app = getApp()
var reqHelp = require('../../utils/req.js');
import Toast from '@vant/weapp/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userId: 0,
        cardId: 0, // '246176',22807
        frontImg: '../../static/images/auth2.png',
        realNameGuid: '0'
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this;
        const cardId = options.cardId;
        app.getLogUser(function (uInfo) {
            const {
                userId,
            } = uInfo;
            that.setData({
                userId: userId,
                cardId: cardId
            });
            var imgUrl = options.frontImg;
            if (imgUrl != null && imgUrl.length > 5) {
                that.setData({
                    frontImg: imgUrl,
                });
            }
            var realGuid = options.realNameGuid;
            if (realGuid != null && realGuid.length > 5) {
                that.setData({
                    realNameGuid: realGuid,
                });
            }
        })
    },
    chooseAction(e) {
        let {
            img
        } = e.currentTarget.dataset;
        var that = this;
        const {
            cardId,
            userId,
            realNameGuid
        } = that.data;
        wx.showActionSheet({
            itemList: ['照片', '拍照'],
            success(res) {
                if (res.tapIndex == 0) {
                    that.chooseWxImage(img)
                } else if (res.tapIndex == 1) {
                    // 拍照
                    wx.navigateTo({
                        url: 'takePhoto?group=' + img + '&cardId=' + cardId + '&userId=' + userId + '&realNameGuid=' + realNameGuid,
                    })
                }
                // console.log(res.tapIndex)
            }
        })
    },
    chooseWxImage: function (img) {
        var that = this;
        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album'],
            maxDuration: 30,
            camera: 'back',
            success: function (res) {
                if (img == 'front') {
                    that.upFrontImg(res.tempFiles[0].tempFilePath)
                } else {
                    that.upBackImg(res.tempFiles[0].tempFilePath)
                }
            }
        })
    },
    upFrontImg(imgurl) {
        var that = this;
        Toast.loading({
            duration: 0, 
            message: '上传中..',
        });
        const {
            cardId,
            userId
        } = that.data;
        reqHelp.requestByUpload('Mini/RealName.ashx?action=IdCardFront', {
            cardId: cardId,
            userId: userId
        }, imgurl).then(function (result) {
            Toast.clear();
            var res = JSON.parse(result);
            if (res.getNum == 0) {
                Toast({
                    type: 'success',
                    message: '上传成功',
                    duration: 2500,
                    onClose: () => {
                        that.setData({
                            frontImg: imgurl,
                            realNameGuid: res.realNameGuid
                        })
                    },
                });
            } else {
                Toast({
                    type: 'text',
                    message: res.getMsg,
                    duration: 2500,
                });
            }
        });
    },
    upBackImg(imgurl) {
        var that = this;
        const {
            realNameGuid
        } = that.data;
        if (realNameGuid == "0") {
            Toast('您还没有上传身份证反面');
            return;
        }
        Toast.loading({
            duration: 0, 
            message: '上传中..',
        });
        reqHelp.requestByUpload('Mini/RealName.ashx?action=IdCardBack', {
            realNameGuid: realNameGuid,
        }, imgurl).then(function (result) {
            Toast.clear();
            var res = JSON.parse(result);
            if (res.getNum == 0) {
                Toast({
                    type: 'success',
                    message: '上传成功',
                    duration: 3000,
                    onClose: () => {
                        wx.redirectTo({
                            url: 'uploadVideo?realNameGuid=' + realNameGuid,
                        })
                    },
                });
            } else {
                Toast({
                    type: 'text',
                    message: res.getMsg,
                    duration: 2500,
                });
            }
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})